#include "StdAfx.h"
#include "GameLoop.h"
#include "NetworkController.h"
#include "DataBase.h"

// CTOR & DTOR
GameLoop::GameLoop(OIS::Mouse *mouse, OIS::Keyboard *keyboard, Ogre::Camera	*camera, Ogre::SceneManager* manager)
{
	// Lancement du serveur
	if (!NetworkController::getInstance()->startService(DataBase::getInstance()->getIp(), DataBase::getInstance()->getPort()))
	{
		Ogre::LogManager::getSingletonPtr()->logMessage("Serveur couldn't be launched !!!");
	}

	// Assignation des variables de Ogre
	this->mMouse = mouse;
	this->mKeyboard = keyboard;
	this->mCamera = camera;
	this->mSceneMgr = manager;
	new TextRenderer();

	// Module pour afficher du texte
	TextRenderer::getSingleton().addTextBox("DebugPlayer", "Debug process", 10, 10, 500, 100, Ogre::ColourValue::Green);
	TextRenderer::getSingleton().addTextBox("DebugEnemy", "Debug process", 10, 40, 500, 100, Ogre::ColourValue::Red);

	// Creation des entites du jeu
	this->map = new Map(mSceneMgr);
	this->player = new Player(mSceneMgr);
	this->enemy = new Enemy(mSceneMgr);

	// On definit la lumiere ambiante
	mSceneMgr->setAmbientLight(Ogre::ColourValue(.9f, 0.9f, 0.9f));

	// Creation d'un brouillard
	mSceneMgr->setFog(Ogre::FOG_LINEAR, Ogre::ColourValue(0.9, 0.9, 0.9), 0.0, 30, 300);

	// Creation de la Skybox
	mSceneMgr->setSkyBox(true, "Skybox");
}

GameLoop::~GameLoop(void)
{
	delete this->mMouse;
	delete this->mKeyboard;
}


bool	GameLoop::Update(const Ogre::FrameEvent& evt)
{
	if (mKeyboard->isKeyDown(OIS::KC_ESCAPE))
        return false;

	if (!NetworkController::getInstance()->getStatus())
	{
		Ogre::LogManager::getSingletonPtr()->logMessage("**** Server: Bad status ****");
		return false;
	}

	//////////////////////
	// Update du joueur //
	//////////////////////
	this->player->Update(mMouse, mKeyboard, mCamera, evt, map);

	if (DataBase::getInstance()->getCurrentState() == GameState::INGAME)
	{
		PlayerNetwork p;

		p.m_posX = mCamera->getPosition().x;
		p.m_posY = mCamera->getPosition().z;
		p.m_number = DataBase::getInstance()->getNumberPlayer();

		//std::cout << "X:" << p.m_posX << "  Y:" << p.m_posY << "  Number:" << p.m_number << std::endl;

		DataBase::getInstance()->setCurrentPlayer(p);

		// Texte de debug joueur
		int x = mCamera->getPosition().x;
		int y = mCamera->getPosition().y;
		int z = mCamera->getPosition().z;
		int angle = mCamera->getOrientation().getYaw().valueDegrees();

		std::ostringstream	oss;
		std::string			debugStringPlayer;

		oss << "The player position is: X=" << x << "  Y=" << y << "  Z=" << z << "  Angle=" << angle;
		debugStringPlayer = oss.str();

		TextRenderer::getSingleton().setText("DebugPlayer", debugStringPlayer);
	}

	////////////////////////
	// Update de l'ennemi //
	////////////////////////
	if (DataBase::getInstance()->getSizePlayer() > 0)
	{
		PlayerNetwork pn = DataBase::getInstance()->getElementAt(0);

		if (pn.m_number != DataBase::getInstance()->getNumberPlayer())
		{
			Ogre::Vector3 enemyPos(pn.m_posX, 8, pn.m_posY);
			Ogre::Radian enemyAngle(0.1);
			this->enemy->Update(enemyPos, enemyAngle);

			// Texte de debug ennemi
			std::ostringstream	oss;
			std::string			debugStringEnemy;
			oss << "The enemy position is: X=" << pn.m_posX << "  Y=10 Z=" << pn.m_posY << "  Angle=" << enemyAngle;
			debugStringEnemy = oss.str();
			TextRenderer::getSingleton().setText("DebugEnemy", debugStringEnemy);
		}
	}
	else
	{
		// Texte de debug a vide ennemi
		TextRenderer::getSingleton().setText("DebugEnemy", "");
	}

	return true;
}